//
//  FindCar_Details_TableViewController.m
//  CarEncyclopedia
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 ThighXingClub. All rights reserved.
//

#import "FindCar_Details_TableViewController.h"
#import "FindCar_Details_TableViewCell.h"
#import "ReceiveData.h"
#import "ImageDownLoader.h"
#import "FindCar_Model_TableViewController.h"
#define Url_Str1 @"http://baojia.qichecdn.com/priceapi3.9.2/services/seriesprice/get?brandid="

#define Url_Str2 @"&cityid=110100&salestate=1"
@interface FindCar_Details_TableViewController ()

@property(nonatomic,retain)NSArray *fctlistArray;


@end

@implementation FindCar_Details_TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
//    self.view.backgroundColor=[UIColor redColor];
    
    //注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_Details_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    
   
    NSString *Url_Str3=[NSString stringWithFormat:@"%@",self.receiveID] ;
    
    NSMutableString *Url_Str=[[ NSMutableString alloc] initWithString:Url_Str1];
    [Url_Str appendString:Url_Str3];
    [Url_Str appendString:Url_Str2];
//    NSLog(@"%@",Url_Str);
    
   
    NSURL *Details_Url=[NSURL URLWithString:Url_Str];
    
    NSURLRequest *requset=[NSURLRequest requestWithURL:Details_Url];
    
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            NSDictionary *DetailsDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *resultDic=[DetailsDic  objectForKey:@"result"];
//            NSLog(@"%@",resultDic);
            self.fctlistArray=[resultDic objectForKey:@"fctlist"];
//            NSLog(@"%@",fctlistArray);
            NSDictionary *SeriesDic=[self.fctlistArray objectAtIndex:0];
            NSString *name=[SeriesDic objectForKey:@"name"];
            NSLog(@"%@",name);
            
            
            [self.tableView reloadData];
        }
    }];
    
   
    
    
    
    
    
    
    
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    NSString *Str=[[self.fctlistArray objectAtIndex:section] objectForKey:@"name"];
    
    return Str;
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return self.fctlistArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray *array=[[self.fctlistArray objectAtIndex:section] objectForKey:@"serieslist"];
    
    
    // Return the number of rows in the section.
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    FindCar_Details_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell2"];
    
//    cell.backgroundColor=[UIColor greenColor];
    
    
    cell.ShowCar_name_Label.text= [[[[self.fctlistArray objectAtIndex:indexPath.section] objectForKey:@"serieslist"] objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    //赋值
    
  
    
    NSString *PriceStr= [[[[self.fctlistArray objectAtIndex:indexPath.section] objectForKey:@"serieslist"] objectAtIndex:indexPath.row] objectForKey:@"price"];
    
    NSMutableString *Price=[NSMutableString stringWithFormat:PriceStr,nil];
    [Price appendString:@"万"];
    cell.Show_Price_Label.text=Price;
    
    
    //标题图片
    
    NSString *imgUrl=[[[[self.fctlistArray objectAtIndex:indexPath.section] objectForKey:@"serieslist"] objectAtIndex:indexPath.row] objectForKey:@"imgurl"];
    
    ImageDownLoader *imageDown=[ImageDownLoader new];
    [imageDown requestImageUrl:imgUrl successBlock:^(NSData *Data) {
        cell.ShowCar_Image.image=[UIImage imageWithData:Data];
    } errorBlock:^(NSError *Error) {
        
    }];
    
    
    
    
    // Configure the cell...
    
    return cell;
}

//返回行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //推送界面
    FindCar_Model_TableViewController *FMV_VC=[FindCar_Model_TableViewController new];
    [self.navigationController pushViewController:FMV_VC animated:YES];
    
    //传值
    
    FMV_VC.pvareaid=[[[[self.fctlistArray objectAtIndex:indexPath.section] objectForKey:@"serieslist"] objectAtIndex:indexPath.row] objectForKey:@"id"];
    
    FMV_VC.seriesid=[[[[self.fctlistArray objectAtIndex:indexPath.section] objectForKey:@"serieslist"] objectAtIndex:indexPath.row] objectForKey:@"salestate"];
    
    
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
